<?php
class PNTambonArray extends PNObjectExArray {
  function PNTambonArray($init=null, $where='')
  {
    $this->PNObject();

    $this->_objType       = 'votedatacenter_tambon';
    $this->_objField      = 'id';
    $this->_objPath       = 'form';
    $this->_objJoin[]     = array ( 'join_table'  =>  'votedatacenter_province',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('pro_name'),
                            'compare_field_table' =>  'pro_id',
                            'compare_field_join'  =>  'id');

    $this->_objJoin[]     = array ( 'join_table'  =>  'votedatacenter_district',
                            'join_field'          =>  array('name'),
                            'object_field_name'   =>  array('dis_name'),
                            'compare_field_table' =>  'dis_id',
                            'compare_field_join'  =>  'id');

    $this->_init($init, $where);
  }

  function genSort(){
    $pntables = pnDBGetTables();
    $column = $pntables[$this->_objType.'_column'];

    $desc  = FormUtil::getPassedValue ('desc', null, 'GET');
    $asc  = FormUtil::getPassedValue ('asc', null, 'GET');
    
    if(isset($desc)){
      //return " ORDER BY $column[$desc] desc";
      return " ORDER BY CONVERT( $column[$desc] USING tis620 ) desc";
    }
    if(isset($asc)){
      //return " ORDER BY $column[$asc] asc";
      return " ORDER BY CONVERT( $column[$asc] USING tis620 ) asc";
    }
    
    return " ORDER BY pro_name, dis_name, tam_name ASC";
  }

  function genFilter(){


    $form  = FormUtil::getPassedValue ('form', false);
    $wheres = array();
    if ($form['pro_id'] && intval($form['pro_id'])) $wheres[] = " tam_pro_id = ".$form['pro_id'];
    if ($form['dis_id'] && intval($form['dis_id'])) $wheres[] = " tam_dis_id = ".$form['dis_id'];
    
    $pntables = pnDBGetTables();
    $column  = $pntables[$this->_objType.'_column'];
    
    $id  = FormUtil::getPassedValue ('id', null, 'GET');
    
    if ($id && intval($id)){
      $wheres[] = " $column[id] = ".$id;
    }
    return implode(' AND ', $wheres) ;
  }
  
  function genFilterTambonFromDistrict(){
    $pntables = pnDBGetTables();
    $column  = $pntables[$this->_objType.'_column'];
    
    $id = FormUtil::getPassedValue ('id', FALSE, 'GET');
    $list = FormUtil::getPassedValue ('list', FALSE, 'GET');
    
    if($id&&intval($id)){
      $wheres[] = " $column[dis_id] = ".$id;
    }
    
    if($list){
      $wheres[] = " $column[id] IN (".$list.")";
    }
    return implode(' AND ', $wheres) ;
  }
  function genFilterTambonByProvinceId(){
    $pntables = pnDBGetTables();
    $column  = $pntables[$this->_objType.'_column'];
    
    $id = FormUtil::getPassedValue ('id', FALSE, 'GET');
    $list = FormUtil::getPassedValue ('list', FALSE, 'GET');
    
    if($id&&intval($id)){
      $wheres[] = " $column[pro_id] = ".$id;
      $wheres[] = " $column[dis_id] < 5409";
    }
    
    if($list){
      $wheres[] = " $column[id] IN (".$list.")";
    }
    return implode(' AND ', $wheres) ;
  }
}
